﻿using AutoMapper;
using Entity.WMS.DTO.WarehouseInventory;
using Entity.WMS.View.WarehouseInventory;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.WMS
{
    public class WarehouseInventoryService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public WarehouseInventoryService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取仓库库存单列表
         */
        public async Task<RequestResponseModel<PageResult<WarehouseInventoryView>>> GetWarehouseInventoryList(WarehouseInventorySearchDTO warehouseInventorySearchDTO)
        {
            //筛选
            var query = _context.WarehouseInventoryView
                .WhereIF(a => a.GoodsCode.Contains(warehouseInventorySearchDTO.Goods) || a.GoodsName.Contains(warehouseInventorySearchDTO.Goods) || a.GoodsSpec.Contains(warehouseInventorySearchDTO.Goods), !string.IsNullOrEmpty(warehouseInventorySearchDTO.Goods))
                .WhereIF(a => a.WarehouseCode.Contains(warehouseInventorySearchDTO.Warehouse) || a.WarehouseName.Contains(warehouseInventorySearchDTO.Warehouse), !string.IsNullOrEmpty(warehouseInventorySearchDTO.Warehouse))
                .WhereIF(a => a.WarehousePositionCode.Contains(warehouseInventorySearchDTO.WarehousePosition) || a.WarehousePositionName.Contains(warehouseInventorySearchDTO.WarehousePosition), !string.IsNullOrEmpty(warehouseInventorySearchDTO.WarehousePosition))
                ;

            //返回列表
            var warehouseInventoryList = await query
                .Page(warehouseInventorySearchDTO.CurrentPage, warehouseInventorySearchDTO.PageSize)
                .ToListAsync();

            //计算总数
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<WarehouseInventoryView>(warehouseInventoryList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }
    }
}
